import { _decorator ,Label,Node, Sprite, tween, UITransform, v3} from 'cc';
import { CocosUtils } from '../../../../Framework/Scripts/Utils/CocosUtils';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { AbNames } from '../../Config/ResConfig';
import { IEndInfoZt } from '../proto/gameZt';
import { GameUtils } from '../../../../Framework/Scripts/Utils/GameUtils';
import { GameData } from '../../Data/GameData';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
const { ccclass, property } = _decorator;

/**
 * 主题赛 结算赢了
 */
@ccclass('GameEndWin_Ctr')
export class GameEndWin_Ctr extends UIComponent {
 
    aniNode: Node;
    nodeL: Node;
    nodeR: Node;

    isClick: boolean;
    labTitle: Label;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
        this.setBgAni(0);
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    protected InitUI(){
        this.registerEvent();

        this.nodeL = this.ViewNode('root/spL');
        this.nodeR = this.ViewNode('root/spR');
        this.labTitle = this.ViewComponent('labTitle',Label);
      
        this.aniNode = this.ViewNode('root/spRes');
        this.AddButtonListener('btnBack',this,this.OnBtnCloseClick);
        this.AddButtonListener('root/btn1',this,this.onBtn1Click);
        this.AddButtonListener('root/btn4',this,this.onBtn4Click);

        this.ViewNode('btnBack').active = false;
    }

    protected InitData() {
        this.isClick = true;

        let winSize = CocosUtils.winSize();
        console.log('=========winSize=======',winSize)
        let v3_p1 = v3(-winSize.width / 2 - this.nodeL.getComponent(UITransform).width, this.nodeL.position.y, 0);
        this.nodeL.setPosition(v3_p1);

        let v3_p2 = v3(winSize.width / 2 + this.nodeR.getComponent(UITransform).width, this.nodeR.position.y, 0);
        this.nodeR.setPosition(v3_p2);

        tween()
            .target(this.nodeL)
            .to(0.5, { position: v3(-winSize.width/2-14,this.nodeL.position.y, 0)})
            .start()

        tween()
            .target(this.nodeR)
            .to(0.5, { position: v3(winSize.width/2+14,this.nodeR.position.y, 0)})
            .start()
    }

    protected registerEvent(){
        // director.on(EventMsgKes.msgThemeMatchGameNextGroupRes,this.onThemeMatchGameNextGroupRes,this);
    }

    protected unRegisterEvent(){
        // director.off(EventMsgKes.msgThemeMatchGameNextGroupRes,this.onThemeMatchGameNextGroupRes,this);
    }

    //继续挑战
    protected onBtn1Click(){
        if(this.isClick === false)
            return;
        this.isClick = false;
        //请求下一轮
        globalThis.gameMgr._GameZtMsg.sendNextGroupRes();
    }

    //答题回顾
    protected onBtn4Click(){
        UIMgr.inst.ShowUIView(AbNames.GamePlay,'Prefabs/GameZT/GamePlayRecord',UIType.Default);
    }

    //请求下一轮返回
    protected onThemeMatchGameNextGroupRes(data:any){
        let code = data.code;
        if(code === 0){
            this.node.destroy();
        }
    }

    /**
     * 
     * @param node 
     * @param data 
     * @param isWin 0赢 1输 
     */
    protected  setItemData(node: Node, data: IEndInfoZt){
        let spHead = node.getChildByPath('head/spHead').getComponent(Sprite);
        let labName = node.getChildByName('labName').getComponent(Label)
        let labTime = node.getChildByPath('spBg/labTime').getComponent(Label);
        let labScore = node.getChildByPath('spBg/layout/Label').getComponent(Label);
        // this.setHeadMat(spHead);
        if(this.hasProInObj(data,'avator')){
            this.LoadRemoteImage(data.avator,spHead);
        }
        if(this.hasProInObj(data,'name')){
            labName.string = data.name;
        }
        if(this.hasProInObj(data,'score')){
            labScore.string = data.score.toString();
        }
        if(this.hasProInObj(data,'time')){
            let time = GameUtils.formatTimeForSecond2(data.time);
            if(data.time === 0){
                labTime.string = `用时${0}秒`;
            }else{
                labTime.string = `用时${time}`;
            }
        }
    }

    public setDataWin(users: IEndInfoZt[]){
        this.labTitle.string = "分类主题挑战赛";
        console.log('==========主题赛=setDataWin==============')
        if(users){
            for (let i = 0; i < users.length; i++) {
                let cfg:IEndInfoZt = users[i];
                let node = null;
                if(cfg.userId === GameData.inst.userInfo.userId){
                    node = this.nodeL;
                }else{
                    node = this.nodeR;
                }
                this.setItemData(node,cfg);   
            }
        }
    }
}